package cn.wolfcode.mp.service.impl;

import cn.wolfcode.mp.domain.Employee;
import cn.wolfcode.mp.mapper.EmployeeMapper;
import cn.wolfcode.mp.query.EmployeeQuery;
import cn.wolfcode.mp.service.IEmployeeService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;


@Service
@Transactional // 事务
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper,Employee> implements IEmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    public IPage<Employee> queryPage(EmployeeQuery qo) {
        IPage<Employee> page = new Page<>(qo.getCurrentPage(), qo.getPageSize());
        //条件查询
        QueryWrapper<Employee> wrapper = new QueryWrapper<>();
        //qo需要进行判空,否则查询不出来,查出来的条件为null
        if (qo.getKeyword()!= null && !"".equals(qo.getKeyword())) {
            wrapper.like("name", qo.getKeyword());
        }
        return super.page(page,wrapper);
    }

    @Override
    public Employee selectByUsername(String username) {
        return employeeMapper.selectByUsername(username);
    }

    @Override
    public Employee login(String username, String password) {
        //方式一:
        // if (username != null && !username.trim().equals("")){
        //方式二:
        if (!StringUtils.hasText(username)) {
            throw new RuntimeException("用户名不能为空");
        }
        if (!StringUtils.hasText(password)) {
            throw new RuntimeException("密码不能为空");
        }
        Employee employee = employeeMapper.selectByUsernameAndPassword(username, password);
        if (employee == null) {
            throw new RuntimeException("账号或密码错误");
        }
        /*if (employee.isStatus() == false) {
            throw new RuntimeException("没有权限登陆");
        }*/
        return employee;
    }
}